package com.zk.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import net.sf.json.JSONObject;

public class GetInfoFromDataBaseTurnToJsonType {

	public static void main(String[] args) {
		 Connection con = null;
		    PreparedStatement pre = null;// 创建预编译语句对象，一般都是用这个而不用Statement
		    ResultSet result = null;// 创建一个结果集对象
		    try{
		        Class.forName("oracle.jdbc.driver.OracleDriver");
		        System.out.println("开始尝试连接数据库！");
		        String url = "jdbc:oracle:" + "thin:@10.1.0.237:1521:esbdb";
		        String user = "esb";
		        String password = "esb";
		        con = DriverManager.getConnection(url, user, password);
		        System.out.println("连接成功！");
		        String sql = "select * from A01";
		        pre = con.prepareStatement(sql);// 实例化预编译语句
		        result = pre.executeQuery();// 执行查询，注意括号中不需要再加参数
		        JSONObject js = new JSONObject();
		        while (result.next()){
		        	js.put(result.getString("A0188"), result.getString("A0190"));
		        }
		        System.out.println(js);
		    }
		    catch (Exception e){
		        e.printStackTrace();
		    }
		    finally{
		        try{
		            if (result != null){
		                result.close();
		            }
		            if (pre != null){
		                pre.close();
		            }
		            if (con != null){
		                con.close();
		            }
		            System.out.println("数据库连接已关闭！");
		        }
		        catch (Exception e){
		            e.printStackTrace();
		        }
		    }
	}
}
